package capricornus.components.accordion
{
	import capricornus.components.event.ComponentsEvent;
	import capricornus.components.singleselectgroup.SingleSelectGroup;
	import capricornus.components.sprite.AbstractComponent;
	import capricornus.components.tilelist.TileList;
	import capricornus.core.ClassManager;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class AccordionTitleList extends AbstractComponent
	{
		protected var _strTitle:String;
		protected var _title:Sprite;
		
		protected var _list:TileList;
		
		protected var _group:SingleSelectGroup;
		
		public function AccordionTitleList( title:String, group:SingleSelectGroup )
		{
			_strTitle = title;
			_group = group;
			init();
		}
		
		private function init():void
		{
			_title = ClassManager.getClassInstance( className ) as Sprite;
			addChild( _title );
			(_title.getChildByName("txt") as TextField).text = _strTitle;
			
			_list = new TileList(1);
			_list.isDestroyOnRemove = true;
			_list.setSize( _title.width, 0 );
			_list.setLocation( 0, _title.height + 1 );
			
			_title.addEventListener( MouseEvent.CLICK, onClickTitle, false, 0, true );
			this.addEventListener( MouseEvent.CLICK, onClick, false, 0, true );
		}
		
		public function destroy():void
		{
			if( _list != null )
			{
				_title.removeEventListener( MouseEvent.CLICK, onClickTitle );
				this.removeEventListener( MouseEvent.CLICK, onClick );
				_list.removeAllItem();
				_list.destroy();
				_list = null;
			}
		}
		
		public function set subSelectedIndex( index:int ):void
		{
			if( index >= 0 && index < _list.length )
			{
				_group.selectedItem = _list.getItemAt(index) as AccordionItem;
			}
		}
		
		override public function set selected( value:Boolean ):void
		{
			if( value )
			{
				if( _list.parent==null )
				{
					addChild( _list );
					dispatchEvent( new ComponentsEvent( ComponentsEvent.RESIZE ) );
				}
			}
			else
			{
				if( _list.parent != null )
				{
					removeChild( _list );
					dispatchEvent( new ComponentsEvent( ComponentsEvent.RESIZE ) );
				}
			}
		}
		
		/**
		 * 返回title类名，需重载 
		 * @return 
		 * 
		 */		
		protected function get className():String
		{
			return "MergeLeftTitlePanel";
		}
		
		/**
		 * 设置数据，生成TreeItem,需重载
		 * @param arr
		 * @param isAll
		 * 
		 */		
		public function setData( arr:Array, isAll:Boolean ):void
		{
			_list.removeAllItem();
		}
		
		/**
		 * 是否紧凑，如果true，则高度为实际高度，不留空白, 默认false 
		 * @param value
		 * 
		 */
		private var _isCompact:Boolean = false;
		public function set isCompact( value:Boolean ):void
		{
			_isCompact = value;
		}
		
		override public function set height(value:Number):void
		{
			_height = value;
			_list.setSize( _title.width, value - _title.height-1 );
			_list.refresh();
		}
		
		override public function get height():Number
		{
			if( _list.parent != null )
			{
				if( _isCompact )
				{
					if( _list.contentHeight < _list.height )
					{
						return _list.contentHeight + _title.height + 1;
					}
					return _height;
				}
				return _height;
			}
			return _title.height;
		}
		
		private function onClickTitle( e:MouseEvent ):void
		{
			dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );
		}
		
		private function onClick( e:MouseEvent ):void
		{
			if( e.target != this )
			{
				e.stopImmediatePropagation();
			}
		}
	}
}